package com.tao.base.ui;

import android.content.ComponentName;
import android.content.Intent;
import android.view.View;

import androidx.annotation.Nullable;

import com.tao.base.R;
import com.tao.base.accessibility.MAccessibilityService;
import com.tao.base.app.receiver.MyAdminReceiver;
import com.tao.mvplibrary.mvp.base.BaseActivity;
import com.tao.utilslib.os.OsUtils;
import com.tao.utilslib.os.SettingJumpUtils;
import com.tao.utilslib.os.SettingPermissionUtils;
import com.tao.utilslib.ui.ToastTools;

public class MainActivity extends BaseActivity {
    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    public void access(View view) {
        if (OsUtils.isAccessibilitySettingsOn(this, getPackageName() + "/" + MAccessibilityService.class.getCanonicalName())
        ) {
            ToastTools.showShort(this, "已经授权无障碍");
        } else {
            ToastTools.showShort(this, "请授权无障碍");
            OsUtils.jumpAccessibility(this, 12345);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 12345) {
            ToastTools.showShort(this, "无障碍" + ((OsUtils.isAccessibilitySettingsOn(this, getPackageName() + "/" + MAccessibilityService.class.getCanonicalName())
                    ? "已" : "未") + " 授权"));

        }else if (requestCode == 12346){
            ToastTools.showShort(this, "设备管理器" +
                    (( OsUtils.checkIsPolicy(this, new ComponentName(this, MyAdminReceiver.class)))
                    ? "已" : "未") + " 授权");        
        }else if (requestCode == 12347){
            ToastTools.showShort(this, "状态检测" +
                    (( SettingPermissionUtils.canUsageStats(this))
                    ? "已" : "未") + " 授权");        
        }
    }

    public void admin(View view) {
        ComponentName componentName = new ComponentName(this, MyAdminReceiver.class);
        if (OsUtils.checkIsPolicy(this, componentName)
        ) {
            ToastTools.showShort(this, "已经授权设备管理器");

        } else {
            ToastTools.showShort(this, "请经授权设备管理器");

            OsUtils.registerPolicy(this, componentName, 12346);
        }

    }

    public void status(View view) {
        if (SettingPermissionUtils.canUsageStats(this)){

            ToastTools.showShort(this, "已经授权状态检测");

        } else {
            ToastTools.showShort(this, "请授权状态检测");
            SettingJumpUtils.jumpUSAGE(this ,12347);
        }
        
    }
}
